package com.ghn.cocknovel.utils;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by admin on
 * 2018/4/20.
 * content 时间工具类
 */

public class MDateUtils {
    public final static String DATE_STYLE_1 = "yyyy.MM.dd";
    public final static String DATE_STYLE_2 = "yyyy.MM.dd HH:mm";
    public final static String DATE_STYLE_3 = "yyyy年MM月dd日 HH:mm";
    public final static String DATE_STYLE_4 = "yyyy-MM-dd";
    public final static String DATE_STYLE_5 = "yyyy-MM-dd HH:mm";
    public final static String DATE_STYLE_6 = "yyyy-MM";
    public final static String DATE_STYLE_7 = "MM-dd";
    public final static String DATE_STYLE_8 = "dd";
    public final static String DATE_STYLE_9 = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取当前时间并格式化
     * 要转换的时间格式
     */
    @SuppressLint("SimpleDateFormat")
    public static String getCurrentTime(String formatStyle) {
        return new SimpleDateFormat(formatStyle).format(new Date());
    }

    @SuppressLint("SimpleDateFormat")
    public static String dateParse(String formatStyle, String time) {
        if (TextUtils.isEmpty(formatStyle) || TextUtils.isEmpty(time)) {
            return "";
        }
        try {
            return new SimpleDateFormat(formatStyle).format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }


    /**
     * 获取前n天日期、后n天日期
     *
     * @param distanceDay 前几天 如获取前7天日期则传-7即可；如果后7天则传7
     * @return
     */
    public static String getOldDate(String formatStyle, int distanceDay) {
        Date date = new Date();
        date.setTime(date.getTime() + (1000 * 60 * 60 * 24 * distanceDay));
        return new SimpleDateFormat(formatStyle).format(date);
    }

    /**
     *
     */
    public static String getChangeMonth(String formatStyle, String yearMonth, int month) {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new SimpleDateFormat(formatStyle).parse(yearMonth));
            calendar.add(Calendar.MONTH, month);
            String d = calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1);
            return d;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}
